//
//  ShortVideoViewController.swift
//  UUVideo
//
//  Created by Galaxy on 2024/1/31.
//  Copyright © 2024 qykj. All rights reserved.
//

import UIKit
import WebKit
import Ji
class ShortVideoViewController: BaseViewController {

    var videoArr:[VideoModel] = []
    var pageNum = 1
    var currentIndex = 0
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        getData()
        setNav()
        
    }
    
    func setNav() {
        let copyBtn = UIBarButtonItem.init(image: UIImage.init(systemName: "doc.on.doc"), style: .plain, target: self, action: #selector(copyBtnClick))
        navigationItem.rightBarButtonItem = copyBtn
    }
    
    @objc func copyBtnClick() {
        let model = videoArr[currentIndex]
        UIPasteboard.general.string = model.videoUrl
        Tool.showSystemAlert(viewController: self, title: "提醒", message: "复制成功") {
            
        }
    }
    
    func getData(){
        NetManager().element { element in
            element.url = "https://pin.porn/api/videoInfo/"
            element.parameters = [
                "from_page": self.pageNum,
                "ipp":40
            ]
        }
        .toast(toast: { toast in
            toast.show = false
        })
        .request().dealSucc { resStr in
            let resultDic = resStr.toDictionary()
            let resultArr = resultDic["data"] as! [[String:Any]]
            if resultArr.isEmpty {
                
            }else{
                self.pageNum += 1
                var indexArr:[IndexPath] = []
                for item in resultArr {
                    let indexPath = IndexPath(row: self.videoArr.count, section: 0)
                    indexArr.append(indexPath)
                    var model = VideoModel()
                    model.videoUrl = item["link"] as! String
                    model.picUrl = item["screen"] as! String
                    self.videoArr.append(model)
                }
                self.mainTableView.beginUpdates()
                self.mainTableView.insertRows(at: indexArr, with: .none)
                self.mainTableView.endUpdates()
            }
        }
        .makeEnd()
    }
    
    lazy var mainTableView: UITableView = {
        let tableView = UITableView.init(frame: .zero, style: .plain)
        view.addSubview(tableView)
        tableView.rowHeight = screenH
        tableView.register(UINib.init(nibName: "ShortVideoTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "cell")
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        tableView.showsVerticalScrollIndicator = false
        tableView.isPagingEnabled = true
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    private func checkIndex(index:Int){
        if index == videoArr.count-10 {
            self.getData()
        }
    }
}

extension ShortVideoViewController:UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return videoArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! ShortVideoTableViewCell
        return cell
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let cell1 = cell as! ShortVideoTableViewCell
        cell1.model = self.videoArr[indexPath.row]
        // 记录当前查询到第几页
        currentIndex = indexPath.row
        checkIndex(index: indexPath.row)
    }
    func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let cell1 = cell as! ShortVideoTableViewCell
        cell1.videoPlayer.stop() 
    }
}
